package com.hatch.utils
{	

	public class MathUtils
	{
		public function MathUtils()
		{
		}
		
		public static function roundToNearest(roundTo:Number, value:Number):Number
		{
			return Math.round(value/roundTo)*roundTo;
		}
		
		public static function randomizeArray(p_aTheArray:Array):Array
		{
			/*
			m_aRArray = []; 
			while (p_aTheArray.length>0)
			{ 
				m_aRArray.push(p_aTheArray.splice(random(p_aTheArray.length), 1)); 
			} 
			return m_aRArray;
			 */
			 return [];
		}

		public static function getRandom(minNum:Number, maxNum:Number, int:Boolean = true):Number
		{
			var n:Number = Math.random() * (maxNum - minNum + 1) + minNum;
			
			if(int)
				return Math.floor(n);
			else
				return n;
		}

		public static function drawFromHat(...args):* 
		{
			var options:Array = [];
			options = options.concat(args);
			return options[MathUtils.getRandom(0, options.length - 1)];
		}

		public static function getNumberAsHexString(number:uint, minimumLength:uint = 1):String 
		{
			// A hexit is the base-16 equivelant of a digit. 
			var hexit:uint = 0;
			// The string that will be output at the end of the function.
			var string:String = "";
                
			// otherwise, loop through all hexits in the number
			while (number > 0 || minimumLength > 0) 
			{
				minimumLength--;
                        
				// get the first hexit from the number by masking the rest
				hexit = number & 0xF;
				// convert the digits over 9 into letters
				switch(hexit) 
				{
					case 0xA: 
						string = "A" + string; 
						break;
					case 0xB: 
						string = "B" + string; 
						break;
					case 0xC: 
						string = "C" + string; 
						break;
					case 0xD: 
						string = "D" + string; 
						break;
					case 0xE: 
						string = "E" + string; 
						break;
					case 0xF: 
						string = "F" + string; 
						break;
					default: 
						string = hexit.toString() + string;
				}
				// erase the hexit from the original number
				number >>>= 4;
			}
                
			return "0x" + string;
		}

		public static function getNextNumber(n:uint, total:uint, base:uint = 0):uint
		{
			return (n == total) ? base : ++n;
		}

		public static function getPrevNumber(n:uint, total:uint, base:uint = 0):uint
		{
			return (n == base) ? total : --n;
		}
	}
}